const WebSocket = require('ws');
const readline = require('readline');

// Connect to the WebSocket server
const ws = new WebSocket('ws://localhost:8080');

// Setup readline to read from console
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.on('line', (input) => {
    if (ws.readyState === WebSocket.OPEN) {
        console.log(`You are sending: ${input}`);
        ws.send(input); // Send the input to the server
    } else {
        console.log('Connection is not open yet.');
    }
});

ws.on('open', () => {
    console.log('Connected to the server!');
    ws.send('Hello, server!');
});

ws.on('message', (message) => {
    console.log(`Server says: ${message}`);
});

ws.on('close', () => {
    console.log('Disconnected from the server.');
});
